package com.tjetc.view;


import com.tjetc.domain.Admin;
import com.tjetc.domain.Customer;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AdminService;
import com.tjetc.service.CustomerService;
import com.tjetc.view.AdminView;
import com.tjetc.view.CustomerView;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

import java.util.Scanner;

/**
 * 用户主界面
 *
 */
public class ClientMainView {
    private static Scanner sc = new Scanner(System.in);
    private static AdminService adminService=ClientProxy.getProxyInstance(AdminService.class);
    private static CustomerService customerService=ClientProxy.getProxyInstance(CustomerService.class);

    public void start() {
        while (true) {
            System.out.println("🥰🥰~~~~~~主页面~~~~~~🥰🥰");
            System.out.println("1.管理员登录");
            System.out.println("2.客户登录");
            System.out.println("3.客户注册");
            System.out.println("0.退出系统");
            System.out.println("请输入你的选择0~3");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    adminLogin();
                    break;
                case 2:
                    customerLogin();
                    break;
                case 3:
                    customerRegister();
                    break;
                case 0:
                    System.exit(0);
                default:
                    System.out.println("请重新输入");
            }
        }
    }

    /**
     * 注册用户
     */
    private static void customerRegister() {
        System.out.println("请输入用户名");
        String account = sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        /*System.out.println("请输入姓名");
        String name = sc.next();
        String phone = null;
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            if (!phone.matches("1[35789]//d{9}")){
                System.out.println("输入错误");
            }else {
                break;
            }
        }*/
        //创建对象
        Customer customer=new Customer();
        customer.setAccount(account);
        customer.setPassword(password);
        /*customer.setName(name);
        customer.setPhone(phone);*/

        Result result=customerService.register(customer);
        System.out.println(result.getMsg());
    }

    /**
     * 用户登录
     */
    private static void customerLogin() {
        System.out.println("请输入用户名");
        String account = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        Customer customer = new Customer();
        customer.setAccount(account);
        customer.setPassword(password);

        Result result = customerService.login(customer);
        System.out.println(result.getMsg());
        if (result.getCode() == 200) {
            AppData.customer = (Customer) result.getData();
            CustomerView customerView=new CustomerView();
            customerView.start();
        }
    }

    /**
     * 管理员登录
     */
    private static void adminLogin() {
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        Result result = adminService.login(admin);
        if (result.getCode()==200) {
            System.out.println(result.getMsg());
            AppData.admin=(Admin)result.getData();
            AdminView adminView =new AdminView();
            adminView.start();
        }else {
            System.out.println(result.getMsg());
        }
    }
}
